package com.neusoft.neuiotms.common.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.common.dto.SerialNumberDTO;
import com.neusoft.neuiotms.common.service.SerialNumberService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "序列号模板")
@RestController
@RequestMapping(value = "/api/w/serialNumber")
public class SerialNumberController {

    @Autowired
    private SerialNumberService serialNumberService;

    @ApiOperation("查询所有")
    @GetMapping
    public ResultListDTO<SerialNumberDTO> index() {
        final List<SerialNumberDTO> serialNumberDTOs = this.serialNumberService.findAll();
        return ResultListDTO.success(serialNumberDTOs);
    }

    @ApiOperation("分页查询所有")
    @GetMapping("/p")
    public PageResultDTO<SerialNumberDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.serialNumberService.search(pageable, searchable));
    }

    @ApiOperation("创建序列号模板")
    @PostMapping("")
    public ResultDTO<SerialNumberDTO> create(@RequestBody final SerialNumberDTO dto) {
        final SerialNumberDTO result = this.serialNumberService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "修改序列号模板")
    @PutMapping("/{id}")
    public ResultDTO<SerialNumberDTO> update(@PathVariable final Long id,
            @RequestBody final SerialNumberDTO dto) {
        final SerialNumberDTO result = this.serialNumberService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("查看序列号模板")
    @GetMapping("/{id}")
    public ResultDTO<SerialNumberDTO> details(@PathVariable final Long id) {
        final SerialNumberDTO dto = this.serialNumberService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除序列号模板")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.serialNumberService.delete(id);
        return ResultDTO.success();
    }
}
